/**
 * 依赖全局变量 InfoGlobal
 * Created by shopnc on 2015/11/26.
 */
//定义表格
var dtGridColumns = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true

    },
    {
        id: 'user.nickName',
        title: '用户昵称',
        type: 'string',
        headerClass: 'text-center width-200',
        columnClass: 'text-center width-200',
        fastQuery: true,
        fastQueryType: 'lk',
        fastSort: false
    },
    {
        id: 'product.name',
        title: '发行人名称',
        type: 'string',
        headerClass: 'text-center width-200',
        columnClass: 'text-center width-200',
        fastQuery: true,
        fastQueryType: 'lk',
        fastSort: false,
        hideType: 'md|sm|xs'
    },
    {
        id: 'product.code',
        title: '代码',
        type: 'string',
        headerClass: 'text-center width-200',
        columnClass: 'text-center width-200',
        fastQuery: true,
        fastQueryType: 'lk',
        fastSort: false,
        hideType: 'md|sm'
    },
    {
        id: 'likeCount',
        title: '点赞数',
        type: 'number',
        headerClass: 'text-left width-200',
        columnClass: 'text-left width-200',
        fastSort: false,
        hideType: 'md|sm|xs'
    },
    {
        id: 'commentCount',
        title: '评论数',
        type: 'number',
        headerClass: 'text-center width-200',
        columnClass: 'text-center width-200',
        fastSort: false,
        hideType: 'md|sm|xs'
    },
    {
        id: 'createTime',
        title: '发表时间',
        type: 'date',
        format: 'yyyy-MM-dd',
        headerClass: 'text-center width-200',
        columnClass: 'text-center width-200',
        fastQuery: true,
        fastQueryType: 'range',
        hideType: 'md|sm'
    },
    {
        id: 'operation',
        title: '管理操作',
        type: 'string',
        columnClass: 'text-center width-400',
        fastSort: false,
        extra: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var html ="";
            if ($("#forumView") && $("#forumView").val() == 1) {
                html += "<a data-target='#lookModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-eye'></i>&nbsp;查看&nbsp;</a>";
            }
            if ($("#forumDel") && $("#forumDel").val() == 1) {
                html += "<a data-target='javascript:;' class='btn btn-danger btn-sm m-r-10'  onclick='forum.delForum(" + record.id + ",\"" + record.user.nickName +"\")'><i class='fa fa-trash-o'></i>&nbsp;删除&nbsp;</a>";
            }
            if (record.type != 2) {
                html += '<a href="javascript:;" name="showChildren" data-dataNo="' + dataNo + '" class="btn btn-white btn-sm"><i class="fa fa-level-down"></i>下级评论</a>';
            }
            return html;
        }
    }
];
var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + '/forum/json/list',
    exportFileName: '论坛列表',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    tools: 'refresh|faseQuery',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int: [], Timestamp: ["createTime"]},
    onGridComplete: function (grid) {

        /**
         * 查看上级论坛
         */
        $("#returnParent").on('click', function () {
            forum.current.gridGoParentForm();
        });
        /**
         * 查看下一级评论按钮事件
         */
        $("a[name='showChildren']").on('click', function () {
            var dataNo = $(this).attr("data-dataNo");
            forumInfo = grid.exhibitDatas[dataNo];
            //bycj[ 设置菜单信息 ]
            forum.current.gridGoLowForm(forumInfo.id);
        });

    }
};
var grid = $.fn.DtGrid.init(dtGridOption);
grid.sortParameter.columnId = ['asc_id'];
grid.fastQueryParameters = new Object();
grid.fastQueryParameters['eq_delFlag'] = 1;

//删除评论URL
var delComUrl = ncGlobal.adminRoot + "comment/json/del";
//删除评论URL
var delForumUrl = ncGlobal.adminRoot + "forum/json/del";
 //判断模糊搜索
var b = true;

var dtGridColumnsComment = [
    {
        id: 'user.nickName',
        title: '评论用户昵称',
        type: 'string',
        headerClass: 'text-center ',
        columnClass: 'text-center ',
        fastQuery: true,
        fastQueryType: 'lk',
        fastSort: false
    },
    {
        id: 'parentUser.nickName',
        title: '被评论用户昵称',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastQuery: true,
        fastQueryType: 'lk',
        fastSort: false
    },
    {
        id: 'content',
        title: '内容',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        hideType: 'md|sm|xs|lg'
    },
    {
        id: 'createTime',
        title: '评论时间',
        type: 'date',
        format: 'yyyy-MM-dd',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastQuery: true,
        fastQueryType: 'range',
        hideType: 'md|sm'
    },
    {
        id: 'operation',
        title: '管理操作',
        type: 'string',
        columnClass: 'text-center width-200',
        fastSort: false,
        extra: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var  html = ""
            if ($("#commentDel") && $("#commentDel").val() == 1) {
                html += "<a data-target='javascript:;' class='btn btn-danger btn-sm m-r-10'  onclick='forum.delComment(" + record.id + ",\"" + record.user.nickName + "\")'><i class='fa fa-trash-o'></i>&nbsp;删除&nbsp;</a>";
            }
            return html;
        }
    }
];




var dtGridOptionComment = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + '/comment/json/list',
    exportFileName: '评论列表',
    columns: dtGridColumnsComment,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    tools: 'refresh|faseQuery',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int: [], Timestamp: ["createTime"]},
    onGridComplete: function (grid) {
    }
};
var grid1 = $.fn.DtGrid.init(dtGridOptionComment);
grid1.sortParameter.columnId = 'id';
grid1.sortParameter.sortType = 1;

var forum = function ($) {
    var current = {
        gridGoLowForm: function (forumId) {
            $("#dtGridContainer").html("")
            $('#keyword').attr("placeholder","请输入评论用户昵称/被评论用户昵称搜索")
            $("#returnParent").removeClass("hidden");
            b=false;
            grid1.fastQueryParameters = new Object();
            grid1.fastQueryParameters['eq_delFlag'] = 1;
            grid1.fastQueryParameters['eq_forumId'] = forumId;
            grid1.pager.startRecord = 0;
            grid1.pager.nowPage = 1;
            grid1.pager.recordCount = -1;
            grid1.pager.pageCount = -1;
            grid1.load();
        },
        gridGoParentForm:function () {
            $("#dtGridContainer").html("")
            $('#keyword').attr("placeholder","请输入用户昵称/发行人名称/代码搜索")
            $("#returnParent").addClass("hidden");
            b=true;
            grid.fastQueryParameters = new Object();
            grid.fastQueryParameters['eq_delFlag'] = 1;
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.load();
        }
    }

    /**
     * 刷新addmodal 上的图片
     */
    function _refreshImage() {
        $(".viewImage").jqthumb({
            width: 180,
            height: 100,
            after: function (imgObj) {
                imgObj.css('opacity', 0).animate({opacity: 1}, 1500);
            }
        });
    }

    /**
     * 删除论坛
     */
    function _delForum(id, name) {
        var tpl = '您确定选择对 <strong>' + name + '</strong> 的论坛进行删除操作吗? 删除后将无法恢复。'
        $.ncConfirm({
            url: delForumUrl,
            data: {
                id: id
            },
            content: tpl,
            alertTitle: "删除操作",
            callBack: function() {
                grid && grid.reload(true)
            }
        });
    }

    /**
     * 删除评论
     */
    function _delComment(id, name) {
        var tpl = '您确定选择对 <strong>' + name + '</strong> 的评论进行删除操作吗? 删除后将无法恢复。'
        $.ncConfirm({
            url: delComUrl,
            data: {
                id: id
            },
            content: tpl,
            alertTitle: "删除操作",
            callBack: function() {
                grid1 && grid1.reload(true)
            }
        });
    }

    function _bindEvent() {
        //模糊搜索
        $('#customSearch').click(function () {
            if(b){
                grid.fastQueryParameters['lk_user.user_name_or_lk_product.name_or_ls_product.code'] = $('#keyword').val();
                grid.pager.startRecord = 0;
                grid.pager.nowPage = 1;
                grid.pager.recordCount = -1;
                grid.pager.pageCount = -1;
                grid.refresh(true);
            }else{
                grid1.fastQueryParameters['lk_user.user_name_or_lk_parentuser.user_name'] = $('#keyword').val();
                grid1.pager.startRecord = 0;
                grid1.pager.nowPage = 1;
                grid1.pager.recordCount = -1;
                grid1.pager.pageCount = -1;
                grid1.refresh(true);
            }
        });

        // bycj [ 查看对话框调用 ]
        $('#lookModal').on('show.bs.modal', function (event) {
            var    //获取接受事件的元素
                button = $(event.relatedTarget),
                //获取data 参数
                datano = button.data('no'),
                modal = $(this),
                //获取列表框中的原始数据
                gridData = grid.sortOriginalDatas[datano];
                $('#picPath').html("")
               if(gridData.picArr != undefined && gridData.picArr != ''){
                    for (var i = 0; i < gridData.picArr.length; i++) {
                        $('#picPath').append('<div class="clickreply-image m-b-15"><img src="' + ncGlobal.fileRoot + gridData.picArr[i] + '" alt="" class="media-object viewImage"></div>');
                    }
               }else{
                   $('#picPath').append("无")
               }
            $("#contents").html(gridData.content);
            _refreshImage();
        });
    }

    //外部可调用
    return {
        init: function () {
            _bindEvent();
        },
        delComment: _delComment,
        delForum:_delForum,
        current: current
    };
}(jQuery);


$(function () {
    grid.load();
    forum.init();
})
